﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Vikipedia.CommonTools.Subtitles;

namespace Vikipedia.CommonTools.Subtitles
{
    static public  class UriExtentions
    {
        static public IDictionary<FileInfo, SubtitleReader > LocateLocalSubtitles(this Uri root)
        {
            if (root.IsFile)
            {
                var  rootpath = root.LocalPath;
                var filename=Path.GetFileNameWithoutExtension (rootpath);
                var index = filename.IndexOf('.');
                if (index >= 0)
                {
                    var prefix = filename.Substring(0, index);
                    var files = new DirectoryInfo(Path.GetDirectoryName(rootpath)).GetFiles(prefix + ".*");
                    var subs = Subtitles.SubtitleReader.GetSubtitles(files);
                    return subs;
                }
                else
                {
                    return null;
                }

            }
            else return null;
        }
    }
}
